import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/house/buliding_list.dart';
import 'package:flutter_enjoy_plus/pages/house/house_detail.dart';
import 'package:flutter_enjoy_plus/pages/house/house_form.dart';
import 'package:flutter_enjoy_plus/pages/house/house_list.dart';
import 'package:flutter_enjoy_plus/pages/house/location_list.dart';
import 'package:flutter_enjoy_plus/pages/house/room_list.dart';
import 'package:flutter_enjoy_plus/pages/notFound/index.dart';
import 'package:flutter_enjoy_plus/pages/profile/index.dart';
import 'package:flutter_enjoy_plus/utils/logger.dart';
import 'package:flutter_enjoy_plus/utils/token.dart';

import '../pages/login/index.dart';
import '../pages/notice/index.dart';
import '../pages/tab_bar_page.dart';

class RouterManager {
  static Map<String, WidgetBuilder> routes = {
    '/': (context) => const TabBarPage(),
    '/detail': (context) => const NoticeDetail(),
    '/login': (context) => const LoginPage(),
  };
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final tokenManager = TokenManager();
    final token = tokenManager.getToken() ?? '';
    //settings.name 访问的路径
    if (token.isEmpty && settings.name != '/login') {
      return MaterialPageRoute(builder: (context) => const LoginPage());
    }
    //执行到这里的时候,token是存在的
    if (settings.name == '/profile') {
      return MaterialPageRoute(
          builder: (context) =>
              ProfilePage(userInfo: settings.arguments as Map));
    }
    if (settings.name == '/house_list') {
      return MaterialPageRoute(builder: (context) => const HouseList());
    }
    if (settings.name == '/location_list') {
      return MaterialPageRoute(builder: (context) => const LocationList());
    }
    if (settings.name == '/building_list') {
      // print(settings.arguments);
      logger.i(settings.arguments);
      return MaterialPageRoute(
          builder: (context) =>
              BuildingList(point: (settings.arguments as Map)['point']));
    }
    if (settings.name == '/room_list') {
      Map temp = settings.arguments as Map;
      return MaterialPageRoute(
          builder: (context) => RoomList(
                point: temp['point'],
                building: temp['building'],
              ));
    }
    if (settings.name == '/house_form') {
      return MaterialPageRoute(
          builder: (context) => HouseForm(
                params: settings.arguments as Map,
              ));
    }
    if (settings.name == '/house_detail') {
      return MaterialPageRoute(
          builder: (context) => HouseDetail(
                id: (settings.arguments as Map)['id'],
              ));
    }
    //统一处理(哪也不让跳)->->跳转到404页面
    return MaterialPageRoute(builder: (context) => const NotFound());
  }
}
